﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using LEEAAttendanceControl.General;
using LEEAAttendanceControl.Web.Services;
using LEEAAttendanceControl.Web;

namespace LEEAAttendanceControl.Views
{
    public partial class CompanyEdit : ChildWindow
    {
        LEEAContext dc = new LEEAContext();

        public Company currcompany = new Company();

        public CompanyEdit()
        {
            InitializeComponent();
        }

        public CompanyEdit(string title)
        {
            InitializeComponent();

            Title = title;
        }

        public void LoadData(int Id)
        {
            dc.Load(dc.GetCompanyByIdQuery(Id), OnCompanyLoaded, null);

            LoadWorkITimes(Id);
        }

        void LoadWorkITimes(int Id)
        {
            WorkTimeDataSource.QueryParameters.Clear();

            Parameter p = new Parameter();

            p.ParameterName = "companyId";
            p.Value = Id;
            WorkTimeDataSource.QueryParameters.Add(p);

            WorkTimeDataSource.Load();
        }

        private void OnCompanyLoaded(LoadOperation lo)
        {
            currcompany=(Company)lo.Entities.FirstOrDefault();

            if (currcompany == null)
            {
                dc.GetCompanyMaxId(Id =>
                {
                    txtCompanyId.Text = ((Id.Value != null) ? Id.Value.Value.ToString() : No.One.ToString());
                }, null);
                currcompany = new Company();
                dc.Companies.Add(currcompany);
            }

            DataContext = currcompany;
        }

        private void OnChangesSubmitted(SubmitOperation so)
        {
            if (so.HasError)
            {
                so.MarkErrorAsHandled();
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralError
                    , ApplicationStrings.GeneralError, "Error");

                message.Show();
            }
            else
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralSave
                    , ApplicationStrings.GeneralSave, "Save");

                message.Closed += (s, e) =>
                {
                    LoadWorkITimes(currcompany.ID);
                };

                message.Show();
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
           dc.SubmitChanges(OnChangesSubmitted, null);
        }

        private void WorkTimeDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        private void ApplyButton_Click(object sender, RoutedEventArgs e)
        {
            WorkTimeDataSource.SubmittedChanges += OnChangesSubmitted;

            WorkTimeDataSource.SubmitChanges();
        }

        private void OnChangesSubmitted(object obj, SubmittedChangesEventArgs e)
        {
            if (e.HasError)
            {
                e.MarkErrorAsHandled();
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralError
                    , ApplicationStrings.GeneralError, "Error");
                message.Show();
            }
            else
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralSave
                    , ApplicationStrings.GeneralSave, "Save");

                message.Show();
            }
        }
    }
}

